﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Baby.Growup.Common
{
    public static class ExtendMethod
    {
        /// <summary>
        /// 将Json字符串转换为指定的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T ToObject<T>(this string json) where T : class
        {
            if (string.IsNullOrWhiteSpace(json))
            {
                return null;
            }

            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 将对象转换为Json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJsonString(this object obj)
        {
            if (obj == null || obj is DBNull) return string.Empty;

            return JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 将字符串转换为整形
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        public static int ToInt(this object obj, int def = -1)
        {
            if (obj == null || obj is DBNull) return def;
            string str = obj.ToString();

            if (string.IsNullOrWhiteSpace(str)) return def;

            if (obj is decimal || obj is double)
            {
                return (int) obj;
            }

            int ret;
            if (int.TryParse(str, out ret))
            {
                return ret;
            }

            return def;
        }

        /// <summary>
        /// 将字符串转换为Decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj, decimal def = -1)
        {
            if (obj == null || obj is DBNull) return def; 
            string str = obj.ToString();

            if (string.IsNullOrWhiteSpace(str)) return def;

            if (obj is int || obj is double)
            {
                return (decimal) obj;
            }

            decimal ret;
            if (decimal.TryParse(str, out ret))
            {
                return ret;
            }
            return def;
        }


        public static long ToLong(this object obj, long def = -1)
        {
            if (obj == null || obj is DBNull) return def;
            string str = obj.ToString();

            if (string.IsNullOrWhiteSpace(str)) return def;

            if (obj is double || obj is decimal)
            {
                string[] strArr = str.Split('.');
                str = strArr[0];
            }

            long ret;
            if (long.TryParse(str, out ret))
            {
                return ret;
            }
            return def;
        }
    }
}
